package com.yuanlp.shoppingmall.home.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yuanlp.shoppingmall.R;
import com.yuanlp.shoppingmall.home.model.ResultBeanData;
import com.yuanlp.shoppingmall.utils.Constants;

import java.util.List;

/**
 * Created by 原立鹏 on 2017/8/28.
 */

public class SeckillViewHolderAdapter extends RecyclerView.Adapter {

    private final Context context;
    private final List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list;
    private View mItemview;


    public SeckillViewHolderAdapter(Context context, List<ResultBeanData.ResultBean.SeckillInfoBean.ListBean> list) {
        this.context=context;
        this.list=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mItemview = View.inflate(context, R.layout.item_seckill,null);
        return new SeckillViewHolder(mItemview);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SeckillViewHolder seckillViewHolder= (SeckillViewHolder) holder;

        //1、根据位置得到数据
        ResultBeanData.ResultBean.SeckillInfoBean.ListBean listBean = list.get(position);

        //2、绑定数据
        Glide.with(context).load(Constants.IMG_URL+listBean.getFigure()).into(((SeckillViewHolder) holder).iv_figure);
        ((SeckillViewHolder) holder).tv_cover_price.setText(listBean.getCover_price());
        ((SeckillViewHolder) holder).tv_origid_price.setText(listBean.getOrigin_price());

    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    class SeckillViewHolder extends RecyclerView.ViewHolder{

        /**
         * 初始化item_skill这个布局中的控件
         * @param itemView
         */
        ImageView iv_figure;
        TextView tv_cover_price;
        TextView tv_origid_price;
        LinearLayout ll_root;

        public SeckillViewHolder(View itemView) {
            super(itemView);
            //实例化控件
            iv_figure= (ImageView) itemView.findViewById(R.id.iv_figure);
            tv_cover_price= (TextView) itemView.findViewById(R.id.tv_cover_price);
            tv_origid_price= (TextView) itemView.findViewById(R.id.tv_origid_price);

            //这里的itemview代表的就是 在页面上显示的item，因此可以设置这个的点击事件来做点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Toast.makeText(context,"秒杀位置="+getLayoutPosition(),Toast.LENGTH_SHORT).show();
                      if (onSeckillRecyclerView!=null){
                          onSeckillRecyclerView.onItemClick(getLayoutPosition());
                      }
                }
            });
        }
    }

    public interface OnSeckillRecyclerView{
        public void onItemClick(int position);
    }


    private OnSeckillRecyclerView onSeckillRecyclerView;

    public void setOnSeckillRecyclerView(OnSeckillRecyclerView onSeckillRecyclerView) {
        this.onSeckillRecyclerView = onSeckillRecyclerView;
    }


}
